<?php
/**
 * @Description 文件上传处理
 * @Sonkwl Xiong
 * @Date 2023/10/09 15:29:52
 * @GET 
 * 
 * @POST 
 * 
 * @Response 
 *  
 */
namespace Sonkwl\UsePHP;
class filer{
    private static $url=NULL;
    private static $path="files";
    private static $type=["image/png","image/jpg","image/jpeg","image/gif","application/pdf","application/vnd.ms-excel","application/vnd.ms-powerpoint","application/msword","application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"];
    private static $size="20";
    private static $tag="default";

    private static $typeTofile=[
        "image/png"=>"png",
        "image/jpg"=>"jpg",
        "image/jpeg"=>"jpeg",
        "image/gif"=>"gif",
        "application/pdf"=>"pdf",
        "application/vnd.ms-excel"=>"xls",
        "application/vnd.ms-powerpoint"=>"doc",
        "application/msword"=>"ppt",
        "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"=>"xlsx"
    ];

    public static function Get_FileType($type){
        if(!isset(self::$typeTofile[$type])){
            jsoner::Response(array("res"=>"NG","message"=>"文件格式查询失败[{$type}]"));
        }
        return self::$typeTofile[$type];
    }

    public static function Set_url($val){
        self::$url=$val;
    }
    public static function Set_path($val){
        self::$path=$val;
    }
    public static function Set_type($val){
        self::$type=$val;
    }
    public static function Set_type_append($val){
        self::$type=array_merge(self::$type,$val);
    }
    public static function Set_type_image(){
        self::$type=["image/png","image/jpg","image/jpeg","image/gif"];
    }
    public static function Set_type_pdf(){
        self::$type=["application/pdf"];
    }
    public static function Set_type_office(){
        self::$type=["application/vnd.ms-excel","application/vnd.ms-powerpoint","application/msword","application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"];
    }
    public static function Set_size($val){
        self::$size=$val;
    }
    public static function Set_tag($val){
        self::$tag=$val;
    }

    public static function Get_url(){
        if(!is_null(self::$url)){
            return trim(self::$url,"/")."/";
        }
        return "";
    }
    public static function Get_path(){
        self::$path=trim(self::$path,"/");
        if(!is_dir(self::$path)){
            if(!mkdir(self::$path,0777,true)){
                jsoner::Response(array("res"=>"NG","message"=>"创建文件目录失败"));
            }
        }
        if(!is_dir(self::$path.'/'.date("Ymd"))){
            if(!mkdir(self::$path.'/'.date("Ymd"),0777,true)){
                jsoner::Response(array("res"=>"NG","message"=>"创建日期目录失败"));
            }
        }
        return self::$path.'/'.date("Ymd")."/";
    }
    public static function Get_type(){
        if(count(self::$type)==0 || !is_array(self::$type)){
            jsoner::Response(array("res"=>"NG","message"=>"定义文件MIME错误"));
        }
        return self::$type;
    }
    public static function Get_size(){
        if(!is_numeric(self::$size)){
            jsoner::Response(array("res"=>"NG","message"=>"定义文件SIZE错误"));
        }
        return self::$size*1024*1024;
    }
    public static function Get_tag(){
        return self::$tag."_";
    }


    public static function GetFileName($fileType,$fileSize){
        if(!in_array($fileType,self::Get_type())){
            jsoner::Response(array("res"=>"NG","message"=>"上传文件格式错误,请上传".implode(",",self::Get_type())."文件"));
        }
        if($fileSize>self::Get_size()){
            jsoner::Response(array("res"=>"NG","message"=>"文件不能大于".$size."M"));
        }
        return self::Get_Path().self::Get_tag().date("YmdHis").random_int(1,10000).".".self::Get_FileType($fileType);
    }

    //使用入口
    public static function Save($file){
        if(is_array($file) && isset($file["name"])){
            return self::SaveFileArray($file);
        }else{
            return self::SaveFileData($file);
        }
    }

    public static function SaveFileArray($file){
        try {
            $filePath=self::GetFileName($file["type"],$file["size"]);
            move_uploaded_file($file["tmp_name"],$filePath);
            return self::Get_url().$filePath;
        }catch (\Throwable $th) {
            jsoner::Response(array("res"=>"NG","message"=>"文件保存失败"));
        }
    }
    public static function SaveFileData($file){
        preg_match('/data:(\w+?\/\w+?);base64,(.+)$/si',$file,$result);
        if(count($result)!=3){
            return $file;
        }
        $filePath=self::GetFileName($result[1],intval(strlen($result[2]))/1024/1024);
        file_put_contents($filePath,base64_decode($result[2]));
        return self::Get_url().$filePath;
    }
}

//测试正则
// include "../autoload.php";
// use Sonkwl\UsePHP\jsoner;

// $data="";
// filer::Set_type_image();
// echo filer::Save($data);
?>